﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BillRepository.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="BillRepository" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data
{
    using System;
    using System.Data.Objects;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Linq.Expressions;

    using Hisaab.Data.Entities;
    using Hisaab.Data.Filters;
    using Hisaab.Data.Interfaces;

    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    ///  Defines the <seealso cref="BillRepository"/> type.
    /// </summary>
    public class BillRepository : IBillRepository
    {
        /// <summary>
        /// Databse instance to use
        /// </summary>
        private readonly IDatabase _database;

        /// <summary>
        /// Initializes a new instance of the <see cref="BillRepository"/> class.
        /// </summary>
        public BillRepository()
            : this(ServiceLocator.Current.GetInstance<IDatabase>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BillRepository"/> class.
        /// </summary>
        /// <param name="database">The database.</param>
        public BillRepository(IDatabase database)
        {
            Contract.Requires<ArgumentNullException>(database != null);
            _database = database;
        }

        /// <summary>
        /// Gets the filtered.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="pageNo">The page no.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>List of filtered items</returns>
        public IQueryable<Bill> Query(Bill filter, int? pageNo = new int?(), int? pageSize = new int?())
        {
            return _database.Bills
                .Filter(filter)
                .Page(pageNo, pageSize);
        }

        /// <summary>
        /// Queries the specified filter.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="pageNo">The page no.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>List of filtered items</returns>
        public IQueryable<Bill> Filter(Expression<Func<Bill, bool>> filter, int? pageNo = new int?(), int? pageSize = new int?())
        {
            return _database.Bills
                .Where(filter)
                .Page(pageNo, pageSize);
        }

        /// <summary>
        /// Finds the by id.
        /// </summary>
        /// <param name="id">The id of entity</param>
        /// <returns>Entity matching by id</returns>
        public Bill FindById(int id)
        {
            return _database.Bills
                .Filter(new Bill { Id = id })
                .FirstOrDefault();
        }

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns>Id of entity added</returns>
        public int Add(Bill entity)
        {
            _database.Bills.AddObject(entity);
            _database.SaveChanges();
            _database.Refresh(RefreshMode.StoreWins, entity);
            return entity.Id;
        }

        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Update(Bill entity)
        {
            var previousEntity = _database.Bills.Filter(new Bill { Id = entity.Id }).FirstOrDefault();

            if (previousEntity != null)
            {
                previousEntity.Name = entity.Name;
                previousEntity.DueDate = entity.DueDate;
                previousEntity.Paid = entity.Paid;
                previousEntity.Recurring = entity.Recurring;
                previousEntity.RecurringFequency = entity.RecurringFequency;
                previousEntity.ExpiryDate = entity.ExpiryDate;
            }

            _database.SaveChanges();
        }

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Delete(Bill entity)
        {
            var previousEntity = _database.Bills.Filter(new Bill { Id = entity.Id }).FirstOrDefault();

            if (previousEntity != null)
            {
                _database.Bills.DeleteObject(previousEntity);
            }

            _database.SaveChanges();
        }
    }
}